package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: itcast_health
 * @create: 2019-10-23 15:45
 * @author: li he
 * @description:
 */
@RestController
@RequestMapping("orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("upload")
    @PreAuthorize("hasAnyAuthority('ORDERSETTING')")
    public Result upload(MultipartFile excelFile){
        System.out.println(excelFile);
        try {
            //解析excel格式的文件转换成 string数组 类型 的list集合
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0){
                //把string数组类型的集合 转换成 orderSetting 类型的集合
                ArrayList<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    //创建orderSetting对象
                    OrderSetting orderSetting = new OrderSetting();
                    //给orderSetting对象赋值
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                    //解析日期
                    Date date = sdf.parse(strings[0]);

                    orderSetting.setOrderDate(date);
                    //解析数字
                    orderSetting.setNumber(Integer.parseInt(strings[1]));

                    //把orderSetting对象储存到list集合中
                    orderSettingList.add(orderSetting);
                }
                //调用业务层储存到数据库
                orderSettingService.add(orderSettingList);
                return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
            } else {
                return new Result(false,"导入数据不能为空 ! ! !");
            }
        } catch (RuntimeException e) {
            return new Result(false,MessageConst.IMPORT_ORDERSETTING_FAIL+" : "+e.getMessage());

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.IMPORT_ORDERSETTING_FAIL);
        }

    }
    @RequestMapping("findByMonth")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result findByMonth(String month){

        try {
            List<OrderSetting> orderSettingList = orderSettingService.findByMonth(month);

            List<Map<String,Object>> list = new ArrayList<>();

            SimpleDateFormat sdf = new SimpleDateFormat("dd");

            if (orderSettingList != null && orderSettingList.size() > 0){

                for (OrderSetting orderSetting : orderSettingList) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("date",sdf.format(orderSetting.getOrderDate()));
                    map.put("number",orderSetting.getNumber());
                    map.put("reservations",orderSetting.getReservations());

                    list.add(map);
                }
            }
            return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_ORDERSETTING_FAIL);
        }
    }
    @RequestMapping("edit")
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    public Result edit(@RequestBody OrderSetting orderSetting){
        System.out.println("orderSetting = " + orderSetting);
        try {

            ArrayList<OrderSetting> orderSettingList = new ArrayList<>();
            orderSettingList.add(orderSetting);
            orderSettingService.add(orderSettingList);
            return new Result(true,MessageConst.EDIT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConst.EDIT_ORDERSETTING_FAIL);
        }
    }
}